package channelStatus

import (
	"gitee.com/fierce_wolf/go-fox-edge-common/commEnv"
	"gitee.com/fierce_wolf/go-fox-edge-common/commRedisService"
	"gitee.com/fierce_wolf/go-fox-edge-common/edgeEntity"
)

type channelStatus struct {
}

func (e *channelStatus) getChannelStatus(channelName string) (*edgeEntity.ChannelStatusEntity, error) {
	entity := edgeEntity.Type.ChannelStatusEntity.Instance()
	entity.ChannelName = channelName
	entity.ChannelType = commEnv.Service.ModelName

	// 查找状态实体
	exist, err := commRedisService.GetEntityByServiceKey(entity.Type(), entity.MakeServiceKey())
	if err != nil {
		return nil, err
	}
	if exist == nil {
		return nil, nil
	}

	return exist.(*edgeEntity.ChannelStatusEntity), nil
}

// UpdateParamStatus 更新参数状态
func (e *channelStatus) updateParamStatus(channelName string, key string, value interface{}) error {
	exist, err := e.getChannelStatus(channelName)
	if err != nil {
		return err
	}
	if exist == nil {
		return nil
	}

	exist.ChannelParam[key] = value
	return commRedisService.UpdateEntity(exist)
}
